package com.nvshen.zhangmeiying.instruction;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import com.google.gson.Gson;
import com.nvshen.zhangmeiying.Constants;
import com.nvshen.zhangmeiying.MainTabActivity;
import com.nvshen.zhangmeiying.R;
import com.nvshen.zhangmeiying.model.User;
import com.nvshen.zhangmeiying.utils.SharedPreferencesUtil;

/**
 * Created by lixinbo on 14-10-8.
 */
public class SplashActivity extends Activity
{
    public static String TAG_ARTICLE_ID = "article_id";
    int articleId = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        String articleIdText = getIntent().getStringExtra(TAG_ARTICLE_ID);
        int extraArticleId = getIntent().getIntExtra(TAG_ARTICLE_ID, -1);

        Log.d("article_id", extraArticleId + "");

        if (articleIdText != null)
        {
            articleId = Integer.parseInt(articleIdText);
            Log.d("article_id", articleIdText);
        }
        else
        {
            Log.d("article_id", "no article id");
        }

        PackageManager manager = this.getPackageManager();
        PackageInfo info = null;
        try
        {
            info = manager.getPackageInfo(getPackageName(), 0);
            Constants.VERISON_NAME = info.versionName;
        }
        catch (PackageManager.NameNotFoundException e)
        {
            e.printStackTrace();
        }

        if (SharedPreferencesUtil.getBoolByName(Constants.USER_LOGIN_STATE, this))
        {
            Constants.currentUser = new Gson().fromJson(SharedPreferencesUtil.getStrByName(Constants.USER_CACHE, this), User.class);
//            JPushInterface.setAlias(this, String.valueOf(Constants.currentUser.getId()), new TagAliasCallback()
//            {
//                @Override
//                public void gotResult(int i, String s, Set<String> strings)
//                {
//                }
//            });
        }
        else
        {
            Constants.currentUser = new User();
            Constants.currentUser.setId(0);
            Constants.currentUser.setNickname("尚未登录");
        }

//        if (articleId != -1)
//        {
////            ArticleDetailActivity.getInstance(getContext(), articleId, true);
//            // TODO: 打开详情页
//        }
//        else
//        {
//            new Handler().postDelayed(new Runnable()
//            {
//                @Override
//                public void run()
//                {
//                    Intent intent = new Intent(SplashActivity.this, MainTabActivity.class);
//                    startActivity(intent);
//                    finish();
//                }
//            }, 2500);
//        }

        new Handler().postDelayed(new Runnable()
        {
            @Override
            public void run()
            {
                Intent intent = new Intent(SplashActivity.this, MainTabActivity.class);
                startActivity(intent);
                finish();
            }
        }, 2500);
    }
}